package com.axc.gallery2.edit.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.axc.gallery2.R;
import com.axc.gallery2.album.AlbumListViewActivity;
import com.axc.gallery2.edit.ColorDialog;
import com.axc.gallery2.edit.EditActivity;
import com.axc.gallery2.edit.SaveBitmap;

public class EditRender extends RelativeLayout{
	Context context;
	// 头部
	FrameLayout frameLayout;
	// 头部菜单
	LinearLayout menu;
	// 放置 RenderView的容器在中间
	LinearLayout MyView;
	// 底部菜单
	LinearLayout bottom;
	RelativeLayout.LayoutParams bottomParams;

	CircleRenderView renderView;

	// 以前敲的
	private ColorDialog dialog;
	FrameLayout layout;
	// 一点菜单延伸
	LinearLayout dotMenu;
	HorizontalScrollView scrollView;
	HorizontalScrollView scrollView2;
	HorizontalScrollView scrollView3;
	public RenderAdapter testAdapter = new RenderAdapter();
	// 头部容器
	LinearLayout headContainer;
	// 头部
	LinearLayout secondheadContainer;
	// 顶部容器参数
	LinearLayout.LayoutParams headParams;
	// 第一个ScrollView包含的LinearLayout
	LinearLayout headinner1;
	LinearLayout headinner2;

	// ImageView params
	LinearLayout.LayoutParams viewParams;
	// 包含颜色按钮（ImageView）的设置参数
	LinearLayout.LayoutParams headColorLayoutParams;

	// 显示颜色与消失菜单按钮
	LinearLayout innerCol;
	LinearLayout outterCol;

	// 调整光圈大小
	SeekBar seekBar;
	int circleRaduis = 150;// getWindowManager().getDefaultDisplay().getWidth()/4;

	// 菜单的显示
	LinearLayout changeCircleRadium;
	LinearLayout changeColor;
	LinearLayout menuMiss;
	LinearLayout reset;
	LinearLayout saveMenu;
	LinearLayout backMenu;
	LinearLayout homeMenu;
	int w = 0;

	public void onCreate2(Context context, int w, int h) {
		this.w = w;
		renderView = new CircleRenderView(context, w, h);
		this.context = context;
		scrollView = new HorizontalScrollView(context);
		scrollView2 = new HorizontalScrollView(context);
		// 暂时先不要 s3
		// scrollView3=new HorizontalScrollView(this);
		// 头部容器
		headContainer = new LinearLayout(context);
		headContainer.setOrientation(LinearLayout.VERTICAL);
		// headContainer.setBackgroundColor(Color.LTGRAY);
		headParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);

		// 二级头部容器
		secondheadContainer = new LinearLayout(context);
		secondheadContainer.setOrientation(LinearLayout.VERTICAL);

		// 控制颜色菜单的显示
		TAdjustInnerColor aInner = new TAdjustInnerColor(testAdapter);
		aInner.run();
		// CirclRadiu
		TCirvleRaidu cirvleRaidu = new TCirvleRaidu();
		cirvleRaidu.run();

		// 第一个外部颜色容器
		headinner1 = new LinearLayout(context);
		headinner1.setGravity(Gravity.LEFT);
		// 第二个内部颜色容器
		headinner2 = new LinearLayout(context);
		headinner2.setGravity(Gravity.LEFT);
		headinner2.setId(11111);

		viewParams = new LinearLayout.LayoutParams(30, 30);// ,LinearLayout.HORIZONTAL);

		// 主容器布局
		headColorLayoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);

		// 创建主要容器
		layout = new FrameLayout(context);

		// 第一行菜单
		headinner1.addView(innerCol);
		headinner1.addView(outterCol);
		headinner1.addView(changeCircleRadium);
		headinner1.addView(changeColor);
		headinner1.addView(reset);
		headinner1.addView(saveMenu);
		headinner1.addView(backMenu);
		headinner1.addView(homeMenu);
		headinner1.addView(menuMiss);

		// 第二行菜单
		headinner2.addView(seekBar);
		headinner2.setVisibility(View.INVISIBLE);

		scrollView.addView(headinner1, headColorLayoutParams);
		scrollView2.addView(headinner2, headColorLayoutParams);

		// scrollView3.addView(child);

		scrollView2.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));

		headContainer.addView(scrollView);
		headContainer.addView(scrollView2);
		headContainer.setVisibility(View.INVISIBLE);
		// headContainer.addView(scrollView3);

		// 最外面的linerLayout容器
		secondheadContainer.addView(headContainer, headParams);

	}

	public EditRender(Context context, int w, int h) {
		super(context);
		this.setLayoutParams(new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.FILL_PARENT));
		onCreate2(context, w, h);
		Init(context, w, h);

	}

	public void Init(Context context, int w, int h) {
		// this.context=context;
		// TBottomMenu tBottomMenu=new TBottomMenu();
		// tBottomMenu.run();
		// 头部
		frameLayout = new FrameLayout(context);
		RelativeLayout.LayoutParams frameParams = new RelativeLayout.LayoutParams(
				new RelativeLayout.LayoutParams(
						RelativeLayout.LayoutParams.FILL_PARENT,
						RelativeLayout.LayoutParams.WRAP_CONTENT));
		frameParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		// 中间
		MyView = new LinearLayout(context);

		RelativeLayout.LayoutParams myviewParams = new RelativeLayout.LayoutParams(
				w, h);// RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT));

		myviewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
		myviewParams.addRule(RelativeLayout.CENTER_VERTICAL);

		MyView.addView(renderView);

		// 头部一个点菜单
		// menu=new MenuIcon(context, R.string.render_menu, R.drawable.go_menu);

		// 头部添加点菜单
		LinearLayout.LayoutParams dotpParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		frameLayout.addView(dotMenu, dotpParams);
		frameLayout.addView(secondheadContainer);

		// 底部
		// frameLayout.addView(bottom,frameParams);

		// 加载前准备
		frameLayout.setVisibility(View.VISIBLE);
		// bottom.setVisibility(View.VISIBLE);

		this.addView(MyView, myviewParams);
		this.addView(frameLayout, frameParams);
		// 底部菜单
		// this.addView(bottom,bottomParams);
	}

	// 以前的代码
	// 菜单View的可见与否
	class TAdjustInnerColor implements Runnable {
		RenderAdapter testAdapter = null;

		public TAdjustInnerColor(RenderAdapter testAdapter) {
			this.testAdapter = testAdapter;
		}

		@Override
		public void run() {
			// 展开
			dotMenu = new MenuIcon(context, R.string.render_menu,
					R.drawable.go_icon);
			dotMenu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (headContainer.getVisibility() == View.VISIBLE)
						headContainer.setVisibility(View.INVISIBLE);
					else {
						headContainer.setVisibility(View.VISIBLE);
					}
					dotMenu.setVisibility(View.INVISIBLE);
				}
			});
			innerCol = new MenuIcon(context, R.string.render_circle_outter,
					R.drawable.inner_menu);
			innerCol.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					dialog = new ColorDialog(context, Color.GREEN, "Hello",
							new ColorDialog.OnColorChangedListener() {

								@Override
								public void colorChanged(int color) {
									testAdapter
											.setInnerCOlor(renderView, color);
								}
							});
					dialog.show();
				}
			});
			outterCol = new MenuIcon(context, R.string.render_circle_inner,
					R.drawable.outter_menu);
			outterCol.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					dialog = new ColorDialog(context, Color.YELLOW, "Hello",
							new ColorDialog.OnColorChangedListener() {

								@Override
								public void colorChanged(int color) {
									testAdapter.setOutColor(renderView, color);
								}
							});
					dialog.show();
				}
			});
			// 半径
			changeCircleRadium = new MenuIcon(context,
					R.string.render_circle_radium, R.drawable.radium);
			changeCircleRadium.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Log.w("change", "change");
					if (headinner2.getVisibility() == View.VISIBLE)
						headinner2.setVisibility(View.INVISIBLE);
					else {
						headinner2.setVisibility(View.VISIBLE);
					}
				}
			});
			
			//替换
			changeColor = new MenuIcon(context, R.string.render_changecolor,
					R.drawable.instead_icon);
			changeColor.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub;
					testAdapter.ChangeInOutColor(renderView);
				}
			});

			// 消失
			menuMiss = new MenuIcon(context, R.string.render_dissmiss,
					R.drawable.close_icon);
			menuMiss.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					headContainer.setVisibility(View.INVISIBLE);
					dotMenu.setVisibility(View.VISIBLE);
				}
			});
			// 保存
			saveMenu = new MenuIcon(context, R.string.save,
					R.drawable.save_menu);
			saveMenu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				    EditRender.this.testAdapter.SaveChange(CircleRenderView.cview);
				   
					new SaveBitmap().Save(context, CircleRenderView.bitmap2);
					
				}
			});
			
			// 重设
			reset = new MenuIcon(context, R.string.render_reset,
					R.drawable.earse_icon);
			reset.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					EditRender.this.testAdapter.Reset(CircleRenderView.cview);
				}
			});

			// 返回
			backMenu = new MenuIcon(context, R.string.render_dissmiss,
					R.drawable.back_activity);
			backMenu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					CircleRenderActivity.circlerender.finish();
				}
			});
			// 主菜单
			homeMenu = new MenuIcon(context, R.string.homepage,
					R.drawable.home_activity);
			homeMenu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent=new Intent();
					intent.setClass(CircleRenderActivity.circlerender, AlbumListViewActivity.class);
					CircleRenderActivity.circlerender.startActivity(intent); 

					CircleRenderActivity.circlerender.finish();
					EditActivity.edit1.finish();
				}
			});
		}
	}

	// 设置光圈半径线程
	class TCirvleRaidu implements Runnable {
		@Override
		public void run() {
			// int width = getWindowManager().getDefaultDisplay().getWidth();
			// TODO Auto-generated method stub
			seekBar = new SeekBar(context);
			// seekBar.setVisibility(View.INVISIBLE);
			seekBar.setProgress(150);
			seekBar.setLayoutParams(new LinearLayout.LayoutParams(w,
					LinearLayout.LayoutParams.FILL_PARENT));
			seekBar.setMax(w * 2);// (getWindowManager().getDefaultDisplay().getWidth());
			seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					// TODO Auto-generated method stub
					circleRaduis = progress;
					testAdapter.setCircleRadius(renderView, circleRaduis);
				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}
			});
		}
	}

	/*
	 * class TBottomMenu implements Runnable { int[][] nums1=null; int
	 * startIndex = 0;
	 *//**
	 * 
	 * @param length
	 *            准备添加菜单按钮数目
	 * @param str
	 *            文字
	 * @param img
	 *            图片
	 * @return
	 */
	/*
	 * public void addImageView(int length, int str, int img) { if (nums1 ==
	 * null) { nums1 = new int[length][2]; } nums1[startIndex][0] = str;
	 * nums1[startIndex][1] = img; startIndex++; }
	 * 
	 * @Override public void run() { // TODO Auto-generated method stub // 底部
	 * bottom = new LinearLayout(context); bottomParams = new
	 * RelativeLayout.LayoutParams( new RelativeLayout.LayoutParams(
	 * RelativeLayout.LayoutParams.FILL_PARENT,
	 * RelativeLayout.LayoutParams.WRAP_CONTENT));
	 * bottomParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
	 * 
	 * addImageView(5, R.string.render_option, R.drawable.back_menu);
	 * addImageView(5, R.string.render_save, R.drawable.back_menu);
	 * addImageView(5, R.string.render_reset, R.drawable.back_menu);
	 * addImageView(5, R.string.render_about, R.drawable.back_menu);
	 * addImageView(5, R.string.render_dissmiss, R.drawable.back_menu); } public
	 * void createImageButton(){ for(int i=0;i<nums1.length;i++){ LinearLayout
	 * linear=new MenuIcon(context, nums1[i+1][0],nums1[i+1][1]);
	 * bottom.addView(linear); } }
	 * 
	 * }
	 */

	/**
	 * 
	 * @author Administrator 用线程跑不起来
	 */
	/*
	 * class TonCreate2 implements Runnable { int w, h = 0;
	 * 
	 * public TonCreate2(int w, int h) { // TODO Auto-generated constructor stub
	 * this.w = w; this.h = h; }
	 * 
	 * @Override public void run() { // TODO Auto-generated method stub
	 * onCreate2(context, w, h); }
	 * 
	 * }
	 */
}
